<?php
/**
 * File htdocs/Aplication/Models/updateque.php ini berisi class Updateque
 *
 * PHP version 5.3
 *
 * @category Models
 * @package  Htdocs/Aplication/Models
 * @author   dio ratar <dio.22ratar@gmail.com>
 * @license  FamilyTree
 * (http://en.wikipedia.org/wiki/Proprietary_software)
 * @link     http://www.FamilyTree.dio22r.com/
 */

if ( ! defined('BASEPATH')) exit('No direct script access allowed');


/**
 * pengisian form :
 * tambah :
 * 1. pilih mau nambah anak ato pasangan...
 * 2. pilih pasangan/anaknya siapa....
 * 3. isi form...
 * 4. upload foto... boleh di skip...
 * 5. view bio...
 * 6. close...
 * 
 *  
 *  edit :
 *  1. lansung klik di nama
 *  2. tampil view bio...
 *  3. edit or close...
 *  4. edit : isi form...
 *  5 view bio...
 */

/**
 * Class updateque merupakan class yang berisi perintah-perintah SQL 
 * yang digunakan untuk update.
 *
 * @category Models
 * @package  Htdocs/Aplication/Models
 * @author   dio ratar <dio.22ratar@gmail.com>
 * @license  FamilyTree
 * (http://en.wikipedia.org/wiki/Proprietary_software)
 * @link     http://www.FamilyTree.dio22r.com/
 */
class home_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$this->load->helper("form");
		$this->load->helper("url");
		$this->load->model('db_access');
	}
	
	public function select_user($arrData)
	{
    	$arrWhere = array ();
		foreach ($arrData as $key => $value) {
			$value = $this->db->escape($value);
			$arrWhere[] = $key . " = " . $value;
		}
		
		$strWhere = implode(" AND ", $arrWhere);
		
		$strSql = "SELECT * FROM user"
				. " WHERE " .$strWhere;
				
		$result = $this->db->query($strSql);
		
		$arrRes = $this->db_access->to_array($result);
		
		return $arrRes;
	}
	
	public function update_password($id, $newPwd)
	{
	    $md5pwd = md5($newPwd);
	    
	    $arrData = array (
	        "id_user" => $id,
	        "password" => $md5pwd
	    );
	    
		foreach ($arrData as $key => $value) {
			$arrData[$key] = $this->db->escape($value);
		}
		
		$sql = "UPDATE user SET password = " . $arrData['password'] .
		    " WHERE id_user = " . $arrData['id_user'] ;
		
		
		$result = $this->db->query($sql);
		
		return $result;
	}
	
    /**
     * function retrieve_root merupakan fungsi untuk megambil
     * level paling atas dari pohon keterunan.
     * 
     * return string berupa query / perintah SQL
     */
    public function retrieve_root()
    {
        $query = "SELECT * FROM data_diri t1
                    WHERE t1.id_data_diri NOT IN (
                         SELECT t2.id_data_diri FROM anak t2 )
                    AND t1.tipe = 'trunk'";
    	
        $result = $this->db->query($query);
        $arrRes = $this->db_access->to_array($result);
        
        return $arrRes;
    }
	
	public function retrieve_child($idMerid)
	{
	    
	    $idMerid = $this->db->escape($idMerid);
	    $query = "SELECT t1.* FROM data_diri t1 JOIN anak t2
    			ON t1.id_data_diri = t2.id_data_diri
    			WHERE t2.id_nikah = " . $idMerid;
	    
        $result = $this->db->query($query);
        $arrRes = $this->db_access->to_array($result);
        
        return $arrRes;
	}
	
	public function retrieve_couple($id, $arrData)
	{
        $id = $this->db->escape($arrData['id_data_diri']);
        $query = "SELECT t1.id_nikah, t2.*
        		FROM nikah t1 JOIN data_diri t2 ";
        			
        if ($arrData['jenis_kelamin'] == "P") {
            $query .= "ON t1.pria = t2.id_data_diri
        			WHERE t1.wanita = " . $id;
        } else {
            $query .= "ON t1.wanita = t2.id_data_diri
        			WHERE t1.pria = " . $id;
        }

        $result = $this->db->query($query);
        $arrRes = $this->db_access->to_array($result);
        
        return $arrRes;
	}
	
}